<template>
  <div class="blog-page" @keydown="handleKeydown" tabindex="0" ref="pageRef">
    <!-- 彩蛋提示 -->
    <EasterEggHint :show="showEasterEgg" />

    <!-- 页面头部 -->
    <div class="page-header">
      <div class="header-content">
        <div class="article-info">
          <h1 class="article-title">{{ article.title }}</h1>
        </div>
      </div>
    </div>

    <!-- 文章内容 -->
    <div class="article-container">
      <div class="article-content">
        <!-- Markdown内容 -->
        <div class="markdown-content">
          <BlogMarkdownViewer :value="article.content" />
        </div>
        <!-- 文章底部统计信息 -->
        <div class="article-footer">
          <div class="article-stats">
            <span class="stat-item">
              <span class="stat-icon">📅</span>
              <span class="stat-text">发布时间：{{ article.createTime }}</span>
            </span>
            <span class="stat-item">
              <span class="stat-icon">🏷️</span>
              <span class="stat-text">分类：{{ article.categoryName }}</span>
            </span>
          </div>
        </div>
      </div>

      <!-- 空状态 -->
      <a-empty v-if="!globalLoading && !article.id" class="empty-state" description="文章不存在">
        <template #image>
          <div class="empty-icon">📄</div>
        </template>
        <template #description>
          <div>
            <h3>文章不存在</h3>
            <p>该文章可能已被删除或移动</p>
          </div>
        </template>
      </a-empty>
    </div>
  </div>
</template>

<script setup lang="ts">
  import { ref, onMounted, nextTick } from 'vue';
  import { useRoute, useRouter } from 'vue-router';
  import { message } from 'ant-design-vue';
  import { getPublicArticleDetail } from '/@/api/blog/articles';
  import BlogMarkdownViewer from '/@/components/Markdown/src/BlogMarkdownViewer.vue';
  import EasterEggHint from '../../components/EasterEggHint.vue';
  import { useGlobalLoading } from '/@/hooks/blog/useGlobalLoading';

  defineOptions({
    name: 'BlogArticlesDetailPage',
  });

  const route = useRoute();
  const router = useRouter();
  const pageRef = ref<HTMLElement>();
  const showEasterEgg = ref(false);
  
  // 使用全局加载状态
  const { withLoading, globalLoading } = useGlobalLoading();

  // 文章数据
  const article = ref<any>({});

  // 获取文章详情
  const fetchArticleDetail = async () => {
    const articleId = route.params.id;
    if (!articleId) {
      message.error('文章ID不存在');
      goBack();
      return;
    }

    return withLoading(async () => {
      const res = await getPublicArticleDetail(articleId as string);
      console.log('详情接口返回：', res);
      article.value = res?.result ?? res ?? {};
      // 处理tags字段为数组
      if (typeof article.value.tags === 'string') {
        try {
          article.value.tags = JSON.parse(article.value.tags);
        } catch (e) {
          article.value.tags = [];
        }
      }
      if (!article.value.id) {
        message.error('未找到文章详情');
        goBack();
      }
    });
  };

  // 键盘事件处理
  const handleKeydown = (event: KeyboardEvent) => {
    console.log('🔤 键盘事件触发:', event.key, event.target);
    
    if (event.key === 'ArrowLeft') {
      console.log('⬅️ 检测到左箭头键，准备返回');
      event.preventDefault();
      event.stopPropagation();
      goBack();
    }
  };

  // 返回上一页
  const goBack = () => {
    router.go(-1);
  };

  // 初始化
  onMounted(() => {
    fetchArticleDetail();
    
    // 设置页面焦点
    nextTick(() => {
      if (pageRef.value) {
        pageRef.value.focus();
      }
    });
  });
</script>

<style scoped lang="scss">
  @use '../../css/blog.scss' as *;

  .page-header {
    margin-bottom: 2rem;
  }
  .header-content {
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
  }
  .article-info {
    text-align: center;
  }
  .article-title {
    font-size: 2.8rem;
    font-weight: 900;
    background: linear-gradient(90deg, #40a9ff 0%, #52c41a 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    text-shadow: 0 6px 32px rgba(64, 169, 255, 0.18);
    margin: 0 0 1.5rem 0;
    line-height: 1.2;
    letter-spacing: 1.5px;
    position: relative;
    text-align: center;
  }
  .article-title::after {
    content: '';
    display: block;
    width: 60px;
    height: 4px;
    margin: 1.2rem auto 0 auto;
    border-radius: 2px;
    background: linear-gradient(90deg, #40a9ff 0%, #52c41a 100%);
    opacity: 0.7;
  }
  .article-container {
    max-width: 900px;
    margin: 0 auto;
  }
  .article-content {
    position: relative;
    background: linear-gradient(120deg, rgba(30, 30, 40, 0.97) 60%, rgba(40, 50, 70, 0.92) 100%);
    border-radius: 26px;
    border: 2px solid rgba(64, 169, 255, 0.13);
    box-shadow: 0 12px 48px rgba(24, 144, 255, 0.1), 0 2px 8px rgba(0, 0, 0, 0.18);
    padding: 3.2rem 2.5rem 2.5rem 2.5rem;
    margin-top: 2.2rem;
    min-height: 420px;
    animation: fadeIn 0.8s;
    overflow: hidden;
  }
  .article-content::before {
    content: '';
    display: block;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 6px;
    border-radius: 26px 26px 0 0;
    background: linear-gradient(90deg, #40a9ff 0%, #52c41a 100%);
    opacity: 0.7;
    z-index: 2;
  }
  .markdown-content {
    margin-bottom: 2.2rem;
  }
  .markdown-body {
    color: #fff;
    text-shadow: 0 2px 8px rgba(0, 0, 0, 0.22);
    font-size: 0.95rem;
    line-height: 1.75;
    font-weight: 400;
  }
  .markdown-body :deep(h1),
  .markdown-body :deep(h2),
  .markdown-body :deep(h3) {
    color: #fff;
    text-shadow: 0 2px 8px rgba(0,0,0,0.22);
    font-weight: 700;
    margin-top: 2rem;
    margin-bottom: 1rem;
  }
  .markdown-body :deep(h1) {
    font-size: 2rem;
    border-bottom: 2px solid rgba(255,255,255,0.18);
    padding-bottom: 0.5rem;
  }
  .markdown-body :deep(h2) {
    font-size: 1.6rem;
    border-bottom: 1px solid rgba(255,255,255,0.12);
    padding-bottom: 0.3rem;
  }
  .markdown-body :deep(p) {
    margin-bottom: 1rem;
  }
  .markdown-body :deep(code) {
    background: rgba(24, 144, 255, 0.22);
    color: #fff;
    padding: 0.2rem 0.4rem;
    border-radius: 4px;
    font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
  }
  .markdown-body :deep(pre) {
    background: rgba(0,0,0,0.45);
    color: #fff;
    border-radius: 6px;
    border: 1px solid rgba(255,255,255,0.10);
    padding: 1rem;
    overflow-x: auto;
    margin: 1rem 0;
  }
  .markdown-body :deep(pre code) {
    background: none;
    color: #fff;
    padding: 0;
  }
  .markdown-body :deep(blockquote) {
    border-left: 4px solid #1890ff;
    background: rgba(24, 144, 255, 0.13);
    padding: 1rem;
    margin: 1rem 0;
    border-radius: 0 8px 8px 0;
  }
  .markdown-body :deep(ul),
  .markdown-body :deep(ol) {
    padding-left: 1.5rem;
    margin: 1rem 0;
  }
  .markdown-body :deep(li) {
    margin-bottom: 0.5rem;
  }
  .markdown-body :deep(table) {
    width: 100%;
    border-collapse: collapse;
    margin: 1rem 0;
    background: rgba(255,255,255,0.05);
    border-radius: 8px;
    overflow: hidden;
  }
  .markdown-body :deep(th),
  .markdown-body :deep(td) {
    border: 1px solid rgba(255,255,255,0.10);
    padding: 0.8rem;
    text-align: left;
  }
  .markdown-body :deep(th) {
    background: rgba(24, 144, 255, 0.18);
    font-weight: 600;
  }
  .markdown-body :deep(a) {
    color: #40a9ff;
    text-decoration: none;
    border-bottom: 1px solid transparent;
    transition: border-color 0.3s ease;
  }
  .markdown-body :deep(a:hover) {
    border-bottom-color: #40a9ff;
  }
  .article-footer {
    border-top: 1px solid rgba(64, 169, 255, 0.08);
    padding: 1.8rem 0 2rem 0;
    margin-top: 2.2rem;
    background: rgba(64, 169, 255, 0.03);
    border-radius: 0 0 20px 20px;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  .article-stats {
    display: flex;
    justify-content: center;
    gap: 2.5rem;
    flex-wrap: wrap;
  }
  .stat-item {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    color: #a8c6ff;
    font-size: 0.95rem;
    font-weight: 500;
    letter-spacing: 0.01em;
    text-shadow: 0 1px 4px rgba(64, 169, 255, 0.1);
    background: rgba(64, 169, 255, 0.05);
    border-radius: 8px;
    padding: 0.4rem 1rem;
    transition: all 0.2s ease;
  }
  .stat-item:hover {
    background: rgba(64, 169, 255, 0.1);
    color: #b9e0ff;
  }
  .stat-icon {
    font-size: 1.1rem;
    filter: drop-shadow(0 1px 4px #40a9ff22);
  }
  @media (max-width: 768px) {
    .article-title {
      font-size: 1.5rem;
    }
    .article-content {
      padding: 1.2rem 0.5rem 1rem 0.5rem;
    }
    .markdown-body {
      font-size: 1rem;
    }
    .article-footer {
      padding-top: 1rem;
      margin-top: 1.2rem;
    }
    .article-stats {
      gap: 1rem;
    }
  }
  @keyframes fadeIn {
    from { opacity: 0; transform: translateY(30px);}
    to { opacity: 1; transform: none;}
  }
</style> 